package com.kbbywyclient.ui.activity;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.SetRewardModule;
import com.kbbywyclient.presenter.SetRewardPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.SetRewardContract;
import com.kbbywyclient.ui.pop.PureListPop;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.widget.CustomToolBar;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Jiangzx on 2017/10/31 10:49.
 * 合伙人奖金分成设置
 */

public class SetRewardActivity extends BaseActivity implements SetRewardContract.View{
    public static final String SEP_PERCENT_S="sepPercent";
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;

    @BindView(R.id.rl_percent)
    RelativeLayout rlPercent;
    @BindView(R.id.tv_reward_percent)
    TextView tvRewardPercent;//奖金分成百分比
    @BindView(R.id.view_divider)
    View dividerView;

    @BindView(R.id.bt_commit)
    Button btCommit;

    @BindView(R.id.tv_profit_mode_des)
    TextView tvProfitModeDes;//收益模式说明

    @Inject
    SetRewardPresenter presenter;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_set_reward;
    }

    @Override
    protected void initView() {
        toolBar.setLeftViewOnClickLinster(v->finish());
        String des="假设：A银行信用卡，客户成功办理后赚取收益为50元/张；\n"+
                "如果您是一级合伙人，设置了留5%，那么：\n"+
                "您赚取的奖金分成=50*5%=2.5元/张\n"+
                "您下级的分享办卡返佣=50*95%=47.5元/张\n\n"+
                "如果您是二级合伙人，设置了留6%，那么：\n"+
                "您赚取的奖金分成=50*6%=3元/张\n"+
                "您下级的分享办卡返佣=50*(100%-5%-6%)=44.5元/张";
        tvProfitModeDes.setText(des);
    }

    @Override
    public void initData() {
        String percent=getIntent().getStringExtra(SEP_PERCENT_S);
        if(!TextUtils.isEmpty(percent)){
            tvRewardPercent.setText(
                    String.format(Locale.CHINESE,"%.0f",Double.parseDouble(percent)));
            btCommit.setText("已设置");
            btCommit.setEnabled(false);
        }
    }

    private PopBean popBean;
    private PureListPop<PopBean> percentPop;
    private static class PopBean{
        private PopBean(String name,double value){
            this.name=name;
            this.value=value;
        }
        private String name;
        private double value;

        @Override
        public String toString() {
            return this.name;
        }
    }
    private void showPercentPop(){
        final Context context=SetRewardActivity.this;
        if(percentPop==null){
            List<PopBean> data=new ArrayList<>();
            data.add(new PopBean("请选择",-1));
            for(int i=0;i<=10;i++){
                if(i==0){
                    data.add(new PopBean("不留",0));
                }else{
                    data.add(new PopBean(String.valueOf(i),i));
                }
            }
            percentPop=new PureListPop<>(context, data, popBean -> {
                SetRewardActivity.this.popBean=popBean;
                tvRewardPercent.setText(popBean.name);
                percentPop.dismiss();
            });
            percentPop.setWidth(tvRewardPercent.getWidth()+ StringUtil.dip2px(30));
            percentPop.setHeight((int) (StringUtil.getScreenHeight()*0.45));
            percentPop.setOnDismissListener(() -> {
                tvRewardPercent.setCompoundDrawablesWithIntrinsicBounds(null,
                        null,
                        ContextCompat.getDrawable(context,R.mipmap.icon_green_arrow_down),
                        null);
            });
        }
        if(btCommit.isEnabled()&&!percentPop.isShowing()){
            int[] position=new int[2];
            rlPercent.getLocationOnScreen(position);
            percentPop.showAtLocation(rlPercent,
                    Gravity.NO_GRAVITY,
                    StringUtil.dip2px(30),
                    position[1]+rlPercent.getHeight());
            tvRewardPercent.setCompoundDrawablesWithIntrinsicBounds(null,
                    null,
                    ContextCompat.getDrawable(context,R.mipmap.icon_green_arrow_up),
                    null);
        }
    }

    @OnClick({R.id.tv_reward_percent,R.id.bt_commit})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_reward_percent:
                showPercentPop();
                break;
            case R.id.bt_commit:
                if(popBean==null||popBean.value==-1){
                    ToastUtil.showShort("请选择奖金比例");
                }else{
                    btCommit.setEnabled(false);
                    presenter.setSepPercent(getUser().getSalesID(),popBean.value);
                }
                break;
        }
    }
    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getSetRewardComponent(new SetRewardModule(this)).inject(this);
    }

    @Override
    public void onSepPercentSuccess() {
        //设置奖金分成成功
        ToastUtil.showShort("设置奖金分成成功");
    }

    @Override
    public void onSepPercentFailure(String msg) {
        //设置奖金分成失败
        ToastUtil.showShort(msg);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }
}
